package com.cy.pj.sys.service.impl;

import com.cy.pj.common.pojo.PageObject;
import com.cy.pj.sys.dao.SysLogDao;
import com.cy.pj.sys.pojo.SysLog;
import com.cy.pj.sys.pojo.SysUserDept;
import com.cy.pj.sys.service.SysLogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogDao sysLogDao;

    //@Async 描述的方法为一个切入点方法，此方法会运行在一个新的线程中，
    //这个线程来自于springboot提供的线程池中的一个线程
    @Async
    @Transactional(propagation = Propagation.REQUIRES_NEW) //描述的方法运行在独立事务中
    //@Transactional(propagation = Propagation.REQUIRED) //默认
    @Override
    public void saveObject(SysLog entity) {
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        sysLogDao.insertObject(entity);
    }

    @Override
    public PageObject<SysLog> findPageObjects(String username, Integer pageCurrent) {
        if(pageCurrent==0 || pageCurrent<1)
            throw new IllegalArgumentException("当前页码值无效");
        int pageSize = 8;
        Page<SysUserDept> page = PageHelper.startPage(pageCurrent, pageSize);
        List<SysLog> records = sysLogDao.findPageObjects(username);
        return new PageObject<>(pageCurrent, pageSize, (int) page.getTotal(), records);
    }
}
